package com.company;

// 扩容 演示

import javax.swing.tree.TreeNode;
import java.util.Map;
import java.util.Objects;

public class C04HashMap2 {

   private static Node<String,String>[] oldTab;

   private static int newCap=32;
   private static int oldCap=16;

    public static void main(String[] args){

//        HashMap<String,String> hashMap =new HashMap<>();
//
//        hashMap.put("00","00");
//        hashMap.put("11","11");
//        hashMap.put("22","22");
//        hashMap.put("33","33");
//        hashMap.put("44","44");
//        hashMap.put("55","55");
//        hashMap.put("66","66");
//        hashMap.put("77","77");
//        hashMap.put("88","88");
//        hashMap.put("99","99");
//        hashMap.put("aa","aa");
//        hashMap.put("bb","bb");
//        hashMap.put("cc","cc");
//        hashMap.put("dd","dd");
//        hashMap.put("ee","ee");
//        hashMap.put("ff","ff");
//        hashMap.put("gg","gg");


        oldTab =  new Node[16];

        //        构建链表节点
        Node<String,String> n0_4 = new Node<String,String>(hash("44"),"44",String.valueOf("44"),null);
        Node<String,String> n0_3 = new Node<String,String>(hash("33"),"33",String.valueOf("33"),n0_4);
        Node<String,String> n0_2 = new Node<String,String>(hash("22"),"22",String.valueOf("22"),n0_3);
        Node<String,String> n0_1 = new Node<String,String>(hash("11"),"11",String.valueOf("11"),n0_2);

        oldTab[0]=n0_1;
        oldTab[1]=new Node<String,String>(hash("aa"),"aa",String.valueOf("aa"),null) ;

        Node<String,String>[]  table = resize();
        System.out.println(table.length+"");

    }

    public static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }


    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;

        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }

        public final K getKey()        { return key; }
        public final V getValue()      { return value; }
        public final String toString() { return key + "=" + value; }

        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                        Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
    }


    final static Node<String,String>[] resize() {

        @SuppressWarnings({"rawtypes","unchecked"})
        Node<String,String>[] newTab = (Node<String,String>[])new Node[newCap];

        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) {
                Node<String,String> e;
                if ((e = oldTab[j]) != null) {
                    oldTab[j] = null;
                    if (e.next == null)
//                        newTab[e.hash & (newCap - 1)] = e;
                        newTab[j] = e;
                    else if (e instanceof TreeNode){
//                        ((HashMap.TreeNode<String,String>)e).split(this, newTab, j, oldCap);
                    }

                    else { // preserve order
                        Node<String,String> loHead = null, loTail = null;
                        Node<String,String> hiHead = null, hiTail = null;
                        Node<String,String> next;
                        do {
                            next = e.next;
                            if ((e.hash & oldCap) == 0) {
                                if (loTail == null)
                                    loHead = e;
                                else
                                    loTail.next = e;
                                loTail = e;
                            }
                            else {
                                if (hiTail == null) //★★
                                    hiHead = e;
                                else
                                    // a0(桶位节点或者是头结点) a1→ a2→ a3→ a4 个节点 （a1和a3 移动到同一高位链表中去）
                                    // 假设现在同次循环 hiTail指向a1节点 e指向a3节点
                                    // 【hiTail.next = e】 表示a1.next=a3
                                    hiTail.next = e;
                                // 【 hiTail = e】 表示hiTail临时变量又指向了a3节点 ★★这里为什么要加上这句呢，如★★
                                hiTail = e;
                            }
                        } while ((e = next) != null);
                        if (loTail != null) {
                            loTail.next = null;
                            newTab[j] = loHead;
                        }
                        if (hiTail != null) {
//                        ★★ a3.next=a4 => a3.next=null
                            hiTail.next = null;
                            newTab[j + oldCap] = hiHead;
                        }
                    }
                }
            }
        }
         return newTab;
    }
}
